import java.io.File;
import java.io.FileNotFoundException;
import java.util.Observable;

public class FileMonitor extends Observable implements Runnable {

	private File fileToMonitor = null;
	private long fileSize = 0;
	
	private long noticeTime;
	
	private boolean isRunning;
	
	public FileMonitor(String _pathname, long _noticeTime) throws FileNotFoundException {
		
		fileToMonitor = new File(_pathname);
		
		if(!fileToMonitor.exists()) {
			throw new FileNotFoundException("Le fichier " + _pathname + " n'existe pas.");
		}
		
		fileSize = fileToMonitor.length();
		
		noticeTime = _noticeTime;
	}
	
	@Override
	public void run() {
		
		isRunning = true;
		
		try {
			while(isRunning) {
				
				long fs = fileToMonitor.length();
				
				if(fs > fileSize) {
					fileSize = fs;
					setChanged();
					notifyObservers();
				}
				
				Thread.sleep(noticeTime);
			}
			
		} catch (InterruptedException ex) {
			ex.printStackTrace();
		}

	}
	
	public void stop() {
		isRunning = false;
	}

}
